package com.udi.project.business.utils.excel.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.udi.common.utils.poi.ExcelUtil;
import com.udi.common.utils.poi.Excels;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.project.business.utils.excel.daomain.*;
import com.udi.project.business.utils.excel.service.ExcelService;
import com.udi.project.business.utils.excel.utils.ExcelListUtil;
import com.udi.project.business.utils.excel.utils.Message;
import com.udi.project.business.local.di.domain.DtDi;
import com.udi.project.business.local.di.service.DiMangeService;
import com.udi.project.business.utils.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Admin
 * 对外提供excel下载请求
 */

@Component
@RequestMapping("/download")
public class ExcelController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private DiMangeService diMangeService;

    @Autowired
    private ExcelService excelService;

    @Autowired
    private ExcelListUtil excelListUtil;

    @Autowired
    private Message message;

    /**
    *
     * @Description: DI标识申报表格导出
     * @param
     * @return: com.udi.framework.web.domain.AjaxResult
     * @Date: 2021/8/18 10:11
     * 请求表格 http://localhost:8080/download/logo?id=370
     * 请求下载 http://localhost:8080/common/download?fileName=fb99605a-e893-48b8-a6aa-42cef1c2bbed_标识申报.xlsx&delete=true
     */
    @RequestMapping("/di")
    @ResponseBody
    public AjaxResult logoDownload(@RequestBody String data) {
        /*string转list*/
        List<LogoExcel> list1= JSONArray.parseArray(data,LogoExcel.class);
        List<devicePackageExcel> list2= JSONArray.parseArray(data,devicePackageExcel.class);
        List<deviceStorageExcel> list3= JSONArray.parseArray(data,deviceStorageExcel.class);
        List<deviceClinicalExcel> list4 = JSONArray.parseArray(data,deviceClinicalExcel.class);
        List<DtDi> list5 = JSONArray.parseArray(data,DtDi.class);
        JSONArray array = new JSONArray();
        JSONObject obj1 = new JSONObject();
        JSONObject obj2 = new JSONObject();
        JSONObject obj3 = new JSONObject();
        JSONObject obj4 = new JSONObject();
        obj1.put("list1",list1);
        obj1.put("name","标识");
        obj2.put("list2",list2);
        obj2.put("name","包装标识信息");
        obj3.put("list3",list3);
        obj3.put("name","存储或操作信息");
        obj4.put("list4",list4);
        obj4.put("name","临床尺寸信息");
        array.add(0,obj1);
        array.add(1,obj2);
        array.add(2,obj3);
        array.add(3,obj4);
        Excels excels = new Excels();
        AjaxResult ajaxResult = excels.export(array,"医疗器械批量填报文件"+"_"+getCurrentTime());
        //修改状态码和修改使用用途
        if((Integer)ajaxResult.get("code")==0){
            for (DtDi dtDi:list5) {
                dtDi.setStatus("1");
                dtDi.setUseInfo("标识最小销售单元");
            }
           diMangeService.updateStatu(list5);
        }
        return  ajaxResult;
    }


    /**
     * @Description: DI标识申报表格导入
     * http://localhost:8080/download/excelTest
     * *//*
    @RequestMapping("/excelTest")
    @ResponseBody
    public Object excelTest(){
        try {
            return excelListUtil.parsing();
        } catch (Exception e) {
            e.printStackTrace();
            return message.errorlMessage("失败");
        }
    }*/



    /**UDI上报表格*/
    /**
     * @Description: UDI打印列表【喷码获取数据也必须走此控制器】
     * @param
     * @return: com.udi.framework.web.domain.AjaxResult
     * @Date: 2021/8/18 10:10
     * http://localhost:8080/download/udi
     * http://localhost:8080/common/download?fileName=94ff8258-7593-4469-bca3-083d17b6c5cf_UDI打印.xlsx&delete=true
     */
    @RequestMapping("/udi")
    @ResponseBody
    public AjaxResult UdiDownload(List<UdiExcel> udiExcelList){
        //List<UdiExcel>  udiExcelList = new ArrayList<>();

        System.out.println("udiExcelList----------->"+udiExcelList);

        UdiExcel udiExcel = new UdiExcel();
        udiExcel.setUdi("(01)06903213101141(17)210809(16)350809(21)AAAA");
        udiExcelList.add(udiExcel);
        for(UdiExcel udi:udiExcelList){
            /** 数据核对将调用此接口数据*/
            redisUtils.lSet("PrintGs1",udi.getUdi());
        }
        ExcelUtil<UdiExcel> excelUtil = new ExcelUtil<>(UdiExcel.class);
        return excelUtil.exportExcel(udiExcelList,"UDI打印");
    }


    public static String getCurrentTime() {
        Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        String mon ="";
        if(month>9){mon = "" + month;}else {mon = "0"+month;}
        int da = c.get(Calendar.DATE);
        String day ="";
        if(da>9){day = "" + da;}else {day = "0"+da;}
        int hour = c.get(Calendar.HOUR_OF_DAY);
        String hou ="";
        if(hour>9){hou = "" + hour;}else {hou = "0"+hour;}
        int minute = c.get(Calendar.MINUTE);
        String min="";
        if(minute>9){min = "" + minute;}else {min = "0"+minute;}
        int second = c.get(Calendar.SECOND);
        String sec ="";
        if(second>9){sec = "" + second;}else {sec = "0"+second;}
        String date = year+mon+day+hou+min+sec;

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return date;
    }

}
